﻿using System.Collections.Generic;
using System.Linq;
using log4net;
using SmallBusinessKit.DataAccess.Interfaces;
using SmallBusinessKit.Interfaces;
using SmallBusinessKit.Models.Entities;

namespace SmallBusinessKit.Domains
{
    public class NewsDomain : INewsDomain
    {
        #region Properties

        private static readonly ILog _log = LogManager.GetLogger(typeof(NewsDomain));

        private readonly INewsRepository    _newsRepository;

        #endregion

        #region Methods

        public NewsDomain(INewsRepository newsRepository)
        {
            _newsRepository = newsRepository;
        }

        public IEnumerable<News> GetAllNews()
        {
            var results = _newsRepository.GetMany(i => i.IsVisible).OrderByDescending(i => i.Date).ToList();

            // or you could do:
            //var deux = _newsRepository.GetAsQueryable().Where(i => i.IsVisible).OrderByDescending(i => i.Date).ToList();

            _log.DebugFormat("Retrieved {0} News articles", results.Count);

            results.ForEach(i => i.Content =  i.Content.Substring(0, 256)); // just want abstracts for each on the News page...

            return (results);
        }

        public News GetNewsItem(string id)
        {
            var result = _newsRepository.Get(i => i.Id == id);
            // or...
            // var result = _newsRepository.GetById(id);

            _log.DebugFormat("Retrieved News - Id/Title: {0} / {1}", result.Id, result.Title);

            return (result);
        }

        #endregion
    }
}
